package com.lmaye.task;

import com.lmaye.App;
import com.lmaye.common.BaseResponse;
import com.lmaye.handler.DetectRequestHandler;
import com.lmaye.utils.DateUtils;
import com.lmaye.utils.Encipher;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * -- 服务应用检测健康监控 Task
 *
 * @author lmay.Zhou
 * @date 2019/6/10 11:21 星期一
 * @qq 379839355
 * @email lmay@lmaye.com
 */
@Slf4j
public class DetectRequestTask implements Runnable {
    /**
     * 执行任务
     */
    @SuppressWarnings("unchecked")
    @Override
    public void run() {
        log.info("************************** DetectRequestTask Start **************************");
        Map<String, String> requestProperties = (Map<String, String>) App.APP_PROPERTIES.get("request");
        String host = requestProperties.get("host");
        BaseResponse loginResponse = DetectRequestHandler.login(host + requestProperties.get("loginUrl"),
                requestProperties.get("username"), Encipher.decode(requestProperties.get("password")));
        log.info("loginResponse: {}", loginResponse);
        Map<String, String> data = (Map<String, String>) loginResponse.getData();
        BaseResponse capitalCapitalRs = null;
        if(Objects.equals("000000", loginResponse.getCode()) && Objects.nonNull(data)) {
            capitalCapitalRs = DetectRequestHandler.userCapitalTotal(
                    host + requestProperties.get("req1Url"), data.get("token"));
            log.info("capitalCapitalRs: {}", capitalCapitalRs);
        }
        if(Objects.nonNull(capitalCapitalRs) && Objects.equals("000000", capitalCapitalRs.getCode())
                && Objects.nonNull(capitalCapitalRs.getData())) {
            BaseResponse checkingEarningsRs = DetectRequestHandler.checkingEarningsTotal(
                    host + requestProperties.get("req2Url"), data.get("token"));
            log.info("checkingEarningsRs: {}", checkingEarningsRs);
        }
        log.info("************************** DetectRequestTask End **************************");
    }
}
